package com.zlt.controller;

import com.zlt.entity.ChatMessageDetail;
import com.zlt.entity.ChatMessageMain;
import com.zlt.service.ChatMessageService;
import com.zlt.service.impl.ChatMessageServiceImpl;
import com.zlt.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/chat/*")
public class ChatController extends BaseController {
    private final ChatMessageService service = new ChatMessageServiceImpl();

    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    /**
     * 查询聊天对象列表
     *
     * @param request
     * @param response
     * @return
     */
    public Result chatList(HttpServletRequest request, HttpServletResponse response) {
        //源——用户ID
        String uid = request.getParameter("uid");
        List<ChatMessageMain> list = service.chatList(uid);
        if (list.isEmpty()) {
            return Result.error("没有记录");
        } else return Result.ok("查询成功", list);
    }

    /**
     * 查询指定用户的聊天记录
     *
     * @param request
     * @param response
     * @return
     */
    public Result chatRecordByUid(HttpServletRequest request, HttpServletResponse response) {
        //源——用户ID
        String src = request.getParameter("src");
        //目标——用户ID
        String dest = request.getParameter("dest");

        List<ChatMessageDetail> details = service.chatDetailList(src, dest);
        if (details.isEmpty())
            return Result.error("还没有聊天记录,请开始聊天吧");
        else return Result.ok("查询成功");
    }

}
